package com.example.bookapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;


import com.example.bookapp.R;
import com.example.bookapp.adpter.ClothAdapter;
import com.example.bookapp.bean.ClothBean;
import com.example.bookapp.network.OkHttpUtils;
import com.google.gson.Gson;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class BookRecommendActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView mIvClothList;
    private ClothBean clothbean;
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cloth_recommand);
        initView();
        Request();

    }
    private void ShowClothList(List<ClothBean.ListBean> list) {
        //调用FoodList适配器
        mIvClothList.setAdapter(new ClothAdapter(BookRecommendActivity.this,list));
    }
    private void initView() {
        mIvClothList = findViewById(R.id.lv_clothlist);
        mIvClothList.setOnItemClickListener(this);
        gson=new Gson();

    }

    private void Request() {
        OkHttpUtils okhttpUtils =new OkHttpUtils();
        //查找食物信息
        Call call= okhttpUtils.OKGET("http://192.168.0.103:1010/bookserver/cloth/list");
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                clothbean = gson.fromJson(response.body().string(), ClothBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ShowClothList(clothbean.getList());
                    }
                });
            }
        });
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent=new Intent(BookRecommendActivity.this,ClothDetailActivity.class);

        intent.putExtra("cloth", clothbean.getList().get(position));
        startActivity(intent);
    }

}
